package com.jihox.abookpro;

import java.util.Vector;

import com.jihox.abookpro.R;
import com.jihox.abookpro.domain.BookProject;
import com.jihox.abookpro.domain.Page;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;


public class PhotoEditActivity extends BaseActivity {
	private Button return_btn;
	private Button save_btn;
	private Button photo_edit_btn;
	private Button photo_replace_btn;
	private Button page_up_btn;
	private Button page_down_btn;
	
	private RelativeLayout left_page;
	private RelativeLayout right_page;
	private RelativeLayout titlebar;
	
	private int currentIndex;
	private int pageNum;
	private Vector<Page> pages;
	private BookProject bookProject;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_phtoto_edit);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		init();
	}

	private void init() {
		return_btn = (Button) findViewById(R.id.btn_return);
		save_btn = (Button) findViewById(R.id.btn_save);
		photo_edit_btn = (Button) findViewById(R.id.btn_photo_edit);
		photo_replace_btn = (Button) findViewById(R.id.btn_photo_replace);
		page_up_btn = (Button) findViewById(R.id.btn_page_up);
		page_down_btn = (Button) findViewById(R.id.btn_page_down);
		left_page = (RelativeLayout) findViewById(R.id.left_page_bk);
		right_page = (RelativeLayout) findViewById(R.id.right_page_bk);
		titlebar = (RelativeLayout) findViewById(R.id.titlebar);
		Intent photointent = getIntent();
		currentIndex = photointent.getIntExtra("CurrentIndex", 0);
		pageNum = currentIndex*2;
		Log.i("msg_index", currentIndex+"");
		bookProject = MyApplication.getInstance().getBookProject();
		pages = bookProject.getBook().getPages();
		Log.i("msg", "pagesize"+pages.size());

		float currentScreenRealRatio = 1f;
		if(bookProject.getBook().getPages().get(currentIndex).getType()!= 0 && bookProject.getBook().getPages().get(currentIndex).getType()!=Page.REGULAR){
			currentScreenRealRatio = CurlActivity.coverPageScreenAndRealRatio;
		}else{
			currentScreenRealRatio = CurlActivity.innerPageScreenAndRealRatio;
		}
		//page的宽高
		int page_width = (int)(pages.get(currentIndex).getWidth()*currentScreenRealRatio);
		int page_height = (int)(pages.get(currentIndex).getHeight()*currentScreenRealRatio);
		//设置左边page的位置
		RelativeLayout.LayoutParams left_relLayoutParams = new RelativeLayout.LayoutParams(page_width,page_height);
		left_relLayoutParams.leftMargin = 50;
		left_relLayoutParams.addRule(RelativeLayout.BELOW,titlebar.getId());
		left_relLayoutParams.topMargin = 40;
		left_page.setLayoutParams(left_relLayoutParams);
		
		//设置右边page的位置
		RelativeLayout.LayoutParams right_relLayoutParams = new RelativeLayout.LayoutParams(page_width,page_height);
//		right_relLayoutParams.rightMargin = 50;
//		right_relLayoutParams.addRule(RelativeLayout.LEFT_OF,left_page.getId());
//		right_relLayoutParams.leftMargin = 20;
//		right_relLayoutParams.addRule(RelativeLayout.BELOW,titlebar.getId());
//		right_relLayoutParams.topMargin = 40;
		right_page.setLayoutParams(right_relLayoutParams);

		
		
		if(currentIndex==0){
			left_page.setVisibility(View.INVISIBLE);
			right_page.setVisibility(View.VISIBLE);
			
		}else if(pageNum == pages.size()){
			right_page.setVisibility(View.INVISIBLE);
			left_page.setVisibility(View.VISIBLE);
			
		}else{
			left_page.setVisibility(View.VISIBLE);
			right_page.setVisibility(View.VISIBLE);

		}
		//返回
		return_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

}
